package com.zbc.enums;

import cn.hutool.core.util.ObjUtil;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户角色枚举类
 * @author fangyu
 * @version v1.0.0
 * @since 2025/9/2 00:07
 */
@Getter
public enum UserRoleEnum {

    USER("用户", "user"),
    ADMIN("管理员", "admin");

    private final String text;
    private final String value;

    UserRoleEnum(String text, String value) {
        this.text = text;
        this.value = value;
    }

    private static final Map<String, UserRoleEnum> VALUE_MAP = new HashMap<>();

    static {
        for (UserRoleEnum roleEnum : UserRoleEnum.values()) {
            VALUE_MAP.put(roleEnum.value, roleEnum); // roleEnum.value: user / admin
        }
    }

    /**
     * 根据值获得枚举对象
     *
     * @param value user / admin
     * @return 对应的枚举对象: USER / ADMIN
     */
    public static UserRoleEnum getEnumByValue(String value) {
        if (ObjUtil.isEmpty(value)) {
            return null;
        }
        return VALUE_MAP.get(value);
    }
}